<?php

include_once "ConnectionManager.php";

function findAllEvent() {
    $query = "select EVENT_ID, EVENT_TITLE_EN, EVENT_TITLE_VN, EVENT_CONTENT_EN, EVENT_CONTENT_VN, EVENT_STATUS from GIAAN_EVENT order by EVENT_STATUS DESC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findEventById($id) {
    $query = "select EVENT_ID, EVENT_TITLE_EN, EVENT_TITLE_VN, EVENT_CONTENT_EN, EVENT_CONTENT_VN, EVENT_STATUS from GIAAN_EVENT 
    where EVENT_ID = $id 
    order by EVENT_ID";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getActiveEvent() {
    $query = "select EVENT_ID, EVENT_TITLE_EN, EVENT_TITLE_VN, EVENT_CONTENT_EN, EVENT_CONTENT_VN, EVENT_STATUS, EVENT_FLASH from GIAAN_EVENT 
     where EVENT_STATUS = 1";
    ConnectionManager::getInstance()->setQuery($query);
    $resultSet = ConnectionManager::getInstance()->query();
    return mysql_fetch_array($resultSet);
}

function getLastId() {
    $query = "SELECT LAST_INSERT_ID() as id";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row["id"];
}

function addEvent($title_en, $title_vn, $content_en, $content_vn, $status) {
    $query = "insert into GIAAN_EVENT(EVENT_TITLE_EN, EVENT_TITLE_VN, EVENT_CONTENT_EN, EVENT_CONTENT_VN, EVENT_STATUS, EVENT_FLASH)
    values('$title_en','$title_vn','$content_en','$content_vn',$status, '')";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function updateEventStatus($eventId, $mode) {
    enableEvent($eventId);
    disableEvent($eventId, $mode);
}

function updateEventFlash($eventId, $flashUrl){
    $query = "update GIAAN_EVENT set EVENT_FLASH = '$flashUrl' where EVENT_ID = $eventId";
    echo $query;
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function enableEvent($id) {
    $query = "update GIAAN_EVENT set EVENT_STATUS = 1 where EVENT_ID = $id";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

// mode: 0 for all, 1 for one
function disableEvent($id, $mode) {
    $query = "";
    if ($mode == 0) {
        $query = "update GIAAN_EVENT set EVENT_STATUS = 0 where EVENT_ID <> $id";
    } else {
        $query = "update GIAAN_EVENT set EVENT_STATUS = 0 where EVENT_ID = $id";
    }
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function updateEvent($id, $title_en, $title_vn, $content_en, $content_vn) {
    $query = "update GIAAN_EVENT 
    set EVENT_TITLE_EN = '$title_en', EVENT_TITLE_VN = '$title_vn', EVENT_CONTENT_EN = '$content_en', EVENT_CONTENT_VN = '$content_vn' where EVENT_ID = $id";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

?>